import { Toast, Dialog } from 'vant'

const state = {
  tabsIndex: 0,
  tabbarIndex: 0,
  cart: JSON.parse(localStorage.getItem('cart')) || [],
  totalPrice: 0, // 总价
  totalNum: 0,
  allCheck: true // 全选状态
}
const getters = {
  cartList(state) {
    localStorage.setItem('cart', JSON.stringify(state.cart))
    return state.cart
  },
  tPrice(state) {
    let totalPrice = 0
    state.cart.forEach(v => {
      if (v.check) {
        totalPrice += v.num * v.goods_price
      }
    })
    localStorage.setItem('cart', JSON.stringify(state.cart))
    state.totalPrice = totalPrice
    return state.totalPrice
  },
  tNum(state) {
    let totalNum = 0
    state.cart.forEach(v => {
      if (v.check) {
        totalNum += v.num
      }
    })
    localStorage.setItem('cart', JSON.stringify(state.cart))
    state.totalNum = totalNum
    return state.totalNum
  },
  // 根据每项 判断全选状态
  allCheckStatus(state) {
    // state.allCheck = false
    let allCheck = true
    if (state.cart.some(v => v.check === false)) {
      allCheck = false
    }
    // state.cart.forEach(v => {
    //   if (v.check) {
    //   } else {
    //     allCheck = false
    //   }
    // })
    state.allCheck = allCheck
    return state.allCheck
  }
}
const mutations = {
  initCart(state, cart) {
    if (cart.some(v => v.check === false)) {
      state.allCheck = false
    }
    // state.cart = cart
    // state.collectGoods = collectGoods
  },
  addGoods(state, goodsInfo) {
    console.log('state.cart', state.cart)
    // 2 查找索引,收否有该id商品
    const index = state.cart.findIndex(
      v => v.goods_id === goodsInfo.goods_id
    )
    if (index === -1) { // 如果不存在索引
      goodsInfo.num = 1 // 则创建num属性
      goodsInfo.check = true // 则创建选中状态
      state.cart.push(goodsInfo)
    } else { // 存在
      state.cart[index].num++
    }
    Toast.success('添加成功')
  },
  // 单选
  // 全选
  onAllCheck(state, val) {
    // 先取反-但不知是否
    // state.allCheck = !state.allCheck
    state.allCheck = val
    if (state.allCheck) {
      // 遍历赋值 --同步全选框
      state.cart.forEach(v => { v.check = true })
    } else {
      state.cart.forEach(v => { v.check = false })
    }
  },
  // 加减
  editNum(state, { operation, goods_id }) {
    console.log(operation, goods_id)
    const index = state.cart.findIndex(v => v.goods_id === goods_id)
    console.log('当前点击的index', index)
    if (state.cart[index].num === 1 && operation === -1) {
      Dialog
        .confirm({
          confirmButtonColor: '#eb4450',
          message: '确定删除?'
        })
        .then(() => {
          console.log('拜你个拜')
          state.cart.splice(index, 1)
        })
        .catch(() => console.log('那没事了，已取消操作'))
    } else {
      state.cart[index].num += operation
    }
  }
}
const actions = {
  addGoods({ commit }, goodsInfo) {
    commit('addGoods', goodsInfo)
  },
  onAllCheck({ commit }, val) {
    commit('onAllCheck', val)
  },
  editNum({ commit }, params) {
    commit('editNum', params)
  },
  initCart({ commit }) {
    setTimeout(() => {
      // console.log('初始化state')
      // const cart = JSON.parse(localStorage.getItem('cart')) || []
      // const collectGoods = JSON.parse(localStorage.getItem('collectList')) || []
      // console.log(cart)
      // commit('initCart', cart)
    }, 100)
  }
}

// 不要忘记把state, mutations等暴露出去。
export default {
  state,
  mutations,
  actions,
  getters
}
